//*************************************************************************
// File Name: MemberList.cpp
// This is the implementation file for the member functions
// of Class MemberList
// "Group 3"
// CS 1C
// March 25, 2010
// Group Project: SuperWarehouse
// Due: March 25, 2010
// - KZ's comment
//*************************************************************************

#include "MemberList.h"

//  private class member
//	list

MemberList::MemberList() : list() {
}

MemberList::~MemberList() {
}

void MemberList::addMember(Member *m) {
	list.push(m);
}

void MemberList::removeMember(Member *m) {
	int idx;

	idx = list.findIndexOfElement(m);
	if (idx >= 0) {
		list.removeElementAtIndex(idx);
	}
}

Member *MemberList::findMemberById(int memberID) {
	int i, n;
	Member *curr;

	n = getNumMembers();

	for (i = 0; i < n; i++) {
		curr = list.retrieveElementAtIndex(i);
		if (curr->getMemberNumber() == memberID) {
			return curr;
		}
	}

	return NULL;
}

Member *MemberList::retrieveMemberAtIndex(int idx) {
	return list.retrieveElementAtIndex(idx);
}

MemberList *MemberList::listSortedByID() {
	MemberList *newList = new MemberList();
	Member *m;
	int i, j, n, min, max;
	char buf[200];

	min = max = -1;
	n = list.getNumElements();
	for (i = 0; i < n; i++) {
		m = list.retrieveElementAtIndex(i);
		if (min < 0 || m->getMemberNumber() < min)
			min = m->getMemberNumber();
		if (max < 0 || m->getMemberNumber() > max)
			max = m->getMemberNumber();
	}
	for (i = min; i <= max; i++) {
		for (j = 0; j < n; j++) {
			m = list.retrieveElementAtIndex(j);
			if (m->getMemberNumber() == i) {
				newList->addMember(m);
				sprintf(buf, "Added member ID %d", m->getMemberNumber());
				debugMsg(buf);
				break;
			}
		}
	}
	return newList;
}

int MemberList::getNumMembers() {
	return list.getNumElements();
}

MemberList &MemberList::operator=(const MemberList &rhs) {
	if (this != &rhs) {
		this->list = rhs.list;
	}
	return *this;
}
